package com.ggh.jinjilive.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.dialog.adapter.CreateRoomAdapter;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddItemOnClickListener1;
import com.ggh.jinjilive.mobile.create.ChooseRoom;
import com.ggh.jinjilive.views.GridItemDecoration;
import com.cy.dialog.BaseDialog;

import java.util.ArrayList;
import java.util.List;

public class ChooseRoomDialog extends BaseDialog {

    private RecyclerView rv;
    private LinearLayout ll_import;
    private ImageView diamondIcon;
    private EditText limitInput;
    private Button start_live;
    //房间集合
    private List<ChooseRoom> chooseRoomList = new ArrayList<>();
    private CreateRoomAdapter createRoomAdapter;

    private ChooseRoom mChooseRoom;
    private int position1;
    public ChooseRoomDialog(Context context) {
        super(context);

    }

    public ChooseRoomDialog(Context context, int themeResId) {
        super(context, themeResId);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView(R.layout.dialog_choose_room);
        canceledOnTouchOutside(true);
        animType(AnimInType.BOTTOM);

        rv = findViewById(R.id.rv);
        ll_import = findViewById(R.id.ll_import);
        diamondIcon = findViewById(R.id.diamondIcon);
        limitInput = findViewById(R.id.limitInput);
        start_live = findViewById(R.id.start_live);
        start_live.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String magnitude = "";
                switch (position1){
                    case 1://计时房间
                        if (limitInput.getText().toString().isEmpty()){
                            ToastUtils.make().show("请输入播放时长");
                            return;
                        }else{
                            magnitude = limitInput.getText().toString();
                            ToastUtils.make().show("计时设置成功");
                        }
                        break;
                    case 2://付费房间
                        if (limitInput.getText().toString().isEmpty()){
                            ToastUtils.make().show("请输入收费金额");
                            return;
                        }else{
                            magnitude = limitInput.getText().toString();
                            ToastUtils.make().show("收费设置成功");
                        }
                        break;
                    case 3://密码房间
                        if (limitInput.getText().toString().isEmpty()){
                            ToastUtils.make().show("请输入密码");
                            return;
                        }else{
                            magnitude = limitInput.getText().toString();
                            ToastUtils.make().show("密码设置成功");
                        }
                        break;


                }

                if(null != addItemOnClickListener){
                    addItemOnClickListener.onSkipClicked(position1,mChooseRoom,magnitude);
                }
            }
        });
        createRoomAdapter = new CreateRoomAdapter();
        createRoomAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                position1 = position;
                mChooseRoom = (ChooseRoom) mObject;

                chooseRoomList.clear();
                ChooseRoom chooseRoom = new ChooseRoom();
                chooseRoom.setID(0);
                chooseRoom.setRoomName("普通房");
                chooseRoom.setRoomPicture(R.mipmap.normal_room_icon1);
                chooseRoomList.add(chooseRoom);
                ChooseRoom chooseRoom1 = new ChooseRoom();
                chooseRoom1.setID(1);
                chooseRoom1.setRoomName("计时房");
                chooseRoom1.setRoomPicture(R.mipmap.time_limited_room_icon1);
                chooseRoomList.add(chooseRoom1);
                ChooseRoom chooseRoom2 = new ChooseRoom();
                chooseRoom2.setID(2);
                chooseRoom2.setRoomName("付费房");
                chooseRoom2.setRoomPicture(R.mipmap.diamond_required_room_icon1);
                chooseRoomList.add(chooseRoom2);
                ChooseRoom chooseRoom3 = new ChooseRoom();
                chooseRoom3.setID(3);
                chooseRoom3.setRoomName("密码房");
                chooseRoom3.setRoomPicture(R.mipmap.password_required_room_icon1);
                chooseRoomList.add(chooseRoom3);
                if(position == 0){
                    ll_import.setVisibility(View.GONE);
                    chooseRoomList.get(position).setRoomPicture(R.mipmap.normal_room_icon2);

                }else if(position == 1){
                    limitInput.setHint("请输入每分钟钻石消耗");
                    ll_import.setVisibility(View.VISIBLE);
                    diamondIcon.setVisibility(View.GONE);
                    chooseRoomList.get(position).setRoomPicture(R.mipmap.time_limited_room_icon2);
                }else if(position == 2){
                    limitInput.setHint("请输入入场费");
                    ll_import.setVisibility(View.VISIBLE);
                    diamondIcon.setVisibility(View.VISIBLE);
                    chooseRoomList.get(position).setRoomPicture(R.mipmap.diamond_required_room_icon2);
                }else if(position == 3){
                    limitInput.setHint("请输入房间密码");
                    ll_import.setVisibility(View.VISIBLE);
                    diamondIcon.setVisibility(View.GONE);
                    chooseRoomList.get(position).setRoomPicture(R.mipmap.password_required_room_icon2);
                }
                createRoomAdapter.setAdd(chooseRoomList,mChooseRoom);
            }
        });
        rv.addItemDecoration(new GridItemDecoration(2, ConvertUtils.dp2px(10),true));
        rv.setLayoutManager(new GridLayoutManager(getContext(),2));
        rv.setAdapter(createRoomAdapter);
        findViewById(R.id.ll_choose_room_occlude).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    /**
     * 设置数据
     * @param chooseRoomList
     * @param chooseRoom
     */
    public void setLiveCategory(List<ChooseRoom> chooseRoomList, ChooseRoom chooseRoom){
        if(null != createRoomAdapter){
            this.chooseRoomList = chooseRoomList;
            createRoomAdapter.setAdd(chooseRoomList,chooseRoom);
        }
    }

    public void setTEXT(){
        if(null == limitInput){
            limitInput = findViewById(R.id.limitInput);
        }
        limitInput.setText("");
    }



    private AddItemOnClickListener1 addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener1 addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }
}
